/*
 * 
 * Copyright 2011 visual-analysis-of-cancer.
 * This file is part of visual-analysis-of-cancer.
 * 
 * visual-analysis-of-cancer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * visual-analysis-of-cancer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * visual-analysis-of-cancer:
 * An tool to analye the WHO and GLOBOCAN cancer data.
 * http://code.google.com/p/visual-analysis-of-cancer/
 * 
 * 
 * 
 * Authors: Erik Sniegula, Johannes Dahlke
 * 
 * Module:  Programming in Processing for Visual Analytics
 * Group:   20
 * 
 * Created: Aug. 2011
 * Version: 1.0
 *
 */
 




package de.fuberlin.processing.who.cancer.gui.detailgraph;

import java.util.Collection;
import java.util.Vector;

import processing.core.PApplet;

import controlP5.ControlP5;
import controlP5.ControllerGroup;
import de.fuberlin.processing.who.cancer.data.Database;
import de.fuberlin.processing.who.cancer.data.structs.StructCancerIncidenceOfLocation;
import de.fuberlin.processing.who.cancer.data.structs.StructCancerMortalityOfLocation;
import de.fuberlin.processing.who.cancer.data.structs.StructHealthFinancingOfLocation;
import de.fuberlin.processing.who.cancer.utils.Test;


public class HealthFinancingBarChart extends BarChart {

	private Vector<StructHealthFinancingOfLocation> data;
	private float maxHealthFinancing = 0;
	
	public HealthFinancingBarChart( ControlP5 theControlP5,
			ControllerGroup theParent, String theName, float theX, float theY,
			int theWidth, int theHeight) {
		super( theControlP5, theParent, theName, theX, theY, theWidth, theHeight);
		this.setTitle( "health financing");
	}
	
	
	
	protected void updateScala() {
		maxHealthFinancing = getMaxHealthFinancing();
		if ( isLeftOriented) {
			setLeftScala( "0");
			setRightScala( String.format( "%.1f",maxHealthFinancing));
		} else {
			setRightScala( "0");
			setLeftScala( String.format( "%.1f",maxHealthFinancing));
		}
	}
	
	private float getMaxHealthFinancing() {
		float result = 0;
		for ( StructHealthFinancingOfLocation st : data) {
			if ( st.healthFinancing > result)
				result = st.healthFinancing;
		}
		return result;
	}
	
	

	@Override
	protected void loadData() {
		if ( Test.isUnassigned( data))
			data = new Vector<StructHealthFinancingOfLocation>(); else
		  data.clear();
		data.addAll( db.getHealthFinancingOfLocations( selectedLocations));
		super.loadData();
	}

	@Override
	protected void drawCell( PApplet p, int locationId, int cellWidth, int cellHeight) {
		p.pushStyle();
		p.fill( 0x22);
		float healthFinancing = 0;
		for ( StructHealthFinancingOfLocation location : data) {
			if ( location.locationId == locationId) {
				healthFinancing = location.healthFinancing;
				break;
			}
		}
		float valueWidth = p.map( healthFinancing, 0, maxHealthFinancing, 0, cellWidth);
		if ( isLeftOriented) {
		  p.rect( 0,  0, valueWidth, cellHeight);
		} else {
		  p.rect( cellWidth -valueWidth,  0, valueWidth, cellHeight);
		}

		p.popStyle();
	}
	

}
